/**
 * Created by zhang on 2016/12/14.
 */
var visitorRecord = {
    events:{
        "#visitorRecordBody .search-record-list click" : "searchRecordList",
        "#visitorRecordBody .record-export click" : "recordExport",
        "#visitorRecordBody .search-record click" : "searchRecord"
    },
    init: function () {
        var _this = this;
        document.title = "访客记录 - 访客管理";
        var temp = Util.template($("#visitorRecord-template").html(),{});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events);
        _this.datetime();
        $(".search-record-list").click();
    },
    datetime : function(){
        $(".date-from").datetimepicker({
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: "zh-CN",
            minView :2,
            startView :2,
            pickerPosition: "bottom-left"
        });
    },
    initTable: function (page) {
        var _this = this;
        _this.department = $("#department").val();
        _this.inviterName = $("#inviterName").val();
        _this.name = $("#name").val();
        _this.starttime = $("#starttime").val();
        _this.endtime = $("#endtime").val();
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getVisitorRecordList(),
            data:{
                department : _this.department,
                inviterName : _this.inviterName,
                userName : _this.name,
                startTime : _this.starttime,
                endTime : _this.endtime,
                currentPage:Util.isEmpty(page)?1:page,
                pageSize:pageInfo.pageSize()
            },
            showLoading : false,
            async:false,
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    $.each(data.result.results, function (i, n) {
                        n.caozuo = "<button class='btn btn-blue search-record'>查看</button>";
                        n.visiteTime =  new Date(n.startTime).format("yyyy-MM-dd HH:mm") +"--"+ new Date(n.endTime).format("yyyy-MM-dd HH:mm");
                    });
                    $(".list-box").cakeTable({
                        head: [   
                            {field: "userName",name: "来访姓名"},
                            {field: "sex",name: "性别",rule:{"1":"男","0":"女"}},
                            {field: "idCard",name: "身份证号码"},
                            {field: "department",name:"所属单位"},
                            {field: "inviterName",name: "受访人员"},
                            {field: "visiteTime",name:"来访时间",type:2},
                            {field: "state",name:"状态",rule:{"0":"已预约","1":"访问中","2":"访问结束","3":"已撤销"}},
                            {field: "reason",name:"事由"},
                            {field: "caozuo",name:"操作",type:2}
                        ],
                        rowData:[
                            {key:"id",attrName:"id"}
                        ],
                        columns:data.result.results,
                        sort:false,
                        showColumns :false,
                        pageInfo:data.result.pagination2,
                        align:"center",
                        pageType:"out",
                        page : function(page){
                            _this.initTable(page);
                        },
                        done:function(){}
                    })
                }   
            }
        });
    },
    searchRecordList : function(){
        this.initTable(1);
    },
    getRecordImg : function(id){
        var result=null;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getRecordImg(id),
            showLoading : false,
            async:false,
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    result = data.result;
                }  
            }
        });
        return result;
    },
    searchRecord : function(ele){
        var _this = this,
            id = $(ele).parents("tr").attr("id"),
            recordImg = _this.getRecordImg(id);
        if (recordImg !== true) {
            var recordImgs = interfaceURL.getRecordImg(id);
            pop.dialog({
                popTitle: "采集照片",
                popContent: Util.template($("#seeImg-template").html(), {recordImg : recordImgs}),
                width :　"500px",
                popCancelShow : false,
                onLoad: function () {
                    $(".pop-ok").css("margin-right","0")
                }
            })
        }else{
            pop.mAlert("暂无图片！")
        }
        
    },
    recordExport : function(){
        var pageNum = $(".page-block.active").html(),
            pageSize = $(".pageSize").val(),
            records = $(".records").html();
        if ( records > 0) {
            var para = "?department="+this.department+"&inviterName="+this.inviterName+"&name="+this.name+"&starttime="+this.starttime+"&endtime="+this.endtime+"&pageNum="+pageNum+"&pageSize="+pageSize+"";
            window.open(interfaceURL.exportVisitorRecord(para));
        }else{
            pop.mAlert("暂无记录！");
        }
    }
};